import { useGet, usePost } from './axios';
import type { UseAxiosResponse } from './type';

export interface PropTableResponse<T> {
  total?: number;
  success: boolean;
  data: T | [];
  response?: UseAxiosResponse<T>;
}

interface IProTablePromiseArgs {
  url: string;
  params?: any;
  headers?: any;
}

export async function useProTablePromiseByPost<T>(
  args: IProTablePromiseArgs,
): Promise<PropTableResponse<T>> {
  const response = await usePost<T>(args.url, args.params, args.headers);

  return {
    success: response?.success ?? true,
    data: response?.data ?? [],
    total: response?.totalCount ?? 0,
    response,
  };
}

export async function useProTablePromiseByGet<T>(
  args: IProTablePromiseArgs,
): Promise<PropTableResponse<T>> {
  const response = await useGet<T>(args.url, args.headers);

  return {
    success: response?.success ?? true,
    data: response?.data ?? [],
    total: response?.totalCount ?? 0,
    response,
  };
}
